#include "Shm.hpp"

int main()
{
    key_t Key = ftok(pathname.c_str(), proj_id);
    if (Key < 0)
    {
        perror("ftok");
        exit(1);
    }

    // client端只需获取共享内存即可
    int shmid = shmget(Key, ShmSize, IPC_CREAT | 0666); // 创建共享内存
    if (shmid < 0)
    {
        perror("shmget error!");
        exit(1);
    }

    std::cout << "Key: " << ToHex(Key) << "\n"
              << "Shmid: " << shmid << std::endl;

    // 关联挂接
    char *shmaddr = (char *)shmat(shmid, NULL, 0 ); 
    std::cout << "server shmat success" << std::endl;

    std::string message;
    char buffer[1024];
    // 通信
    while(true)
    {
        fgets(buffer,sizeof(buffer),stdin);
        size_t len = strlen(buffer);
        buffer[len - 1] = '\0';
        strcpy(shmaddr,buffer);

    }

    // 去关联
    shmdt(shmaddr);
    std::cout << "server shmdt success" << std::endl;

    // client端是不需要删除共享内存的，因为共享内存是由server端建立的

    return 0;
}